package pl.minim.unidefExample {
	import com.googlecode.milib.dbg.InstanceDebugServiceProxy;	
	
	import flash.events.TextEvent;	
	
	import fl.data.DataProvider;	
	import fl.controls.TextArea;	
	import fl.controls.List;	
	import fl.controls.Label;	
	
	import flash.events.Event;	
	
	import fl.controls.ComboBox;	

	import pl.minim.unidefCMS.UnidefManager;			import flash.utils.ByteArray;			import com.googlecode.milib.base.MLBaseClass;
	
	import flash.display.MovieClip;	

	/**
	 * @author Marek Brun
	 */
	public class Example extends MLBaseClass {
		
		private var _mc:MovieClip;
		private var unidefData:UnidefManager;
		private var data:Object;
		private var pageSelect:ComboBox;
		private var title:Label;
		private var list:List;
		private var text:TextArea;

		public function Example(mc:MovieClip, xmlba:ByteArray) {
			this._mc=mc;
			unidefData=new UnidefManager();
			unidefData.setDataByByteArray(xmlba);
			
			data=unidefData.exportToRawData();
			
			pageSelect=ComboBox(mc.content.pageSelect);			pageSelect.addEventListener(Event.CHANGE, onPageSelect_Change);
			
			title=Label(mc.content.page.title);
			list=List(mc.content.page.list);
			text=TextArea(mc.content.page.text);
			
			text.addEventListener(TextEvent.LINK, onText_Link);
			
			dbg.log('UnidefCMS data :'+dbg.link(data));
			
			var i:uint;
			var loop:Object;
			for(i=0;i<data.pages.length;i++){
				loop=data.pages[i];
				pageSelect.addItem({label:loop.title, data:loop});
			}
			
			pageSelect.selectedIndex=0;
			setPage(pageSelect.selectedItem.data);
		}
		
		protected function setPage(data:Object):void {
			title.text=data.title;
			var i:uint;
			var loop:Object, listData:DataProvider=new DataProvider();
			for(i=0;i<data.tech.length;i++){
				loop=data.tech[i];
				listData.addItem({label:loop.name, data:loop});
			}
			list.dataProvider=listData;
			var texts:Array=[];
			for(var n:String in data){
				texts.push(n+':'+dbg.link(data[n]));
			}
			text.htmlText=texts.join('<br>');
		}
				public function get mc():MovieClip {  return _mc; }
		
//********************************************************************************************
//		events for Example
//********************************************************************************************
		protected function onPageSelect_Change(event:Event):void {
			dbg.log('seledted page:'+dbg.link(pageSelect.selectedItem.data));
			setPage(pageSelect.selectedItem.data);
		}
		
		protected function onText_Link(event:TextEvent):void {
			InstanceDebugServiceProxy.exeLink(event.text);
		}
	}
}
